/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
AOS.utils.namespace( "AOS.tasks" );

AOS.tasks.initialize = function() {
	AOS.tasks.refresh();
}

AOS.tasks.refresh = function() {
	YAHOO.util.Connect.asyncRequest( "GET", "get_current_task.php", AOS.tasks.currentTaskCallback, null );
}

AOS.tasks.currentTaskSuccess = function( o ) {
	var container = document.getElementById( "current-task" );
	container.innerHTML = o.responseText;
	YAHOO.util.Connect.asyncRequest( "GET", "get_completed_tasks.php", AOS.tasks.completedTasksCallback, null );
	initializeTooltips();
}

AOS.tasks.currentTaskFailure = function( o ) {
	alert( "There was a problem retrieving the current task.\n" +
			"Please, refresh the page to try again." );
}

AOS.tasks.currentTaskCallback = {
	success: AOS.tasks.currentTaskSuccess, 
	failure: AOS.tasks.currentTaskFailure
};

AOS.tasks.completedTasksSuccess = function( o ) {
	var container = document.getElementById( "completed-task-list" );
	container.innerHTML = o.responseText;
}

AOS.tasks.completedTasksFailure = function( o ) {
	alert( "There was a problem retrieving the completed task list.\n" +
			"Please, refresh the page to try again." );
}

AOS.tasks.completedTasksCallback = {
	success: AOS.tasks.completedTasksSuccess, 
	failure: AOS.tasks.completedTasksFailure
};

AOS.tasks.submitTask = function() {
	var nextTaskId = -1;
	var nextTask = document.getElementById( "next-task-1" );
	if( nextTask && nextTask.checked ) {
		nextTaskId = nextTask.value;
	}
	nextTask = document.getElementById( "next-task-2" );
	if( nextTask && nextTask.checked ) {
		nextTaskId = nextTask.value;
	}
	nextTask = document.getElementById( "next-task-3" );
	if( nextTask && nextTask.checked ) {
		nextTaskId = nextTask.value;
	}
	
	var capitalX = -1;
	var capitalY = -1;
	var capitalLocation = document.getElementById( "capital-x" );
	if( capitalLocation && capitalLocation.value ) {
		capitalX = capitalLocation.value;
	}
	capitalLocation = document.getElementById( "capital-y" );
	if( capitalLocation && capitalLocation.value ) {
		capitalY = capitalLocation.value;
	}
	YAHOO.util.Connect.asyncRequest( "post", "../overview/submit_task.php", AOS.tasks.submitTaskCallback, "next_task=" + nextTaskId + "&capital_x=" + capitalX + "&capital_y=" + capitalY );
}

AOS.tasks.submitTaskSuccess = function( o ) {
	var container = document.getElementById( "current-task" );
	container.innerHTML = o.responseText;
	initializeTooltips();
	YAHOO.util.Connect.asyncRequest( "GET", "get_completed_tasks.php", AOS.tasks.completedTasksCallback, null );
}

AOS.tasks.submitTaskFailure = function( o ) {
	alert( "There was a problem submitting this task.\n" +
			"Please, try again or refresh the page." );
}

AOS.tasks.submitTaskCallback = {
	success: AOS.tasks.submitTaskSuccess,
	failure: AOS.tasks.submitTaskFailure
}

YAHOO.util.Event.addListener( window, "load", AOS.tasks.initialize );
